package com.witdata.controller.docker;

import com.witdata.common.domain.Result;
import com.witdata.common.domain.Status;
import com.witdata.common.domain.dto.InstanceContainerDto;
import com.witdata.common.domain.docker.Container;
import com.witdata.framework.service.docker.ContainerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/docker/container")
@RequiredArgsConstructor
public class ContainerController {
    private final ContainerService containerService;

    @PostMapping("/create")
    public Result<String> createContainer(@RequestBody InstanceContainerDto instanceContainerDto) {
        String containerId = containerService.createContainer(instanceContainerDto);
        return Result.ok(Status.CREATESUCCESS, containerId);
    }

    @PostMapping("/start")
    public Result<String> startContainer(@RequestBody InstanceContainerDto instanceContainerDto) {
        containerService.startContainer(instanceContainerDto);
        return Result.ok(Status.STARTDOCKERSUCCESS);
    }

    @PostMapping("/remove")
    public Result<String> removeContainer(@RequestBody InstanceContainerDto instanceContainerDto) {
        containerService.removeContainer(instanceContainerDto);
        return Result.ok(Status.DELETESUCCESS);
    }

    @PostMapping("/stop")
    public Result<String> stopContainer(@RequestBody InstanceContainerDto instanceContainerDto) {
        containerService.stopContainer(instanceContainerDto);
        return Result.ok(Status.STOPDOCKERSUCCESS);
    }

    @GetMapping("/list")
    public Result<List<Container>> getContainerList(@RequestParam("instanceId") Long instanceId) {
        List<Container> containerList = containerService.getContainerList(instanceId);
        return Result.ok(Status.SUCCESS, containerList);
    }

    @GetMapping("/status")
    public Result<Container> getContainerStatus(@RequestParam("instanceId") Long instanceId, @RequestParam("containerId") String containerId) {
        Container container = containerService.getContainerStatus(instanceId, containerId);
        return Result.ok(Status.SUCCESS, container);
    }

}
